
import React from 'react';
import { BasicLayoutProps, Settings as LayoutSettings } from '@ant-design/pro-layout';
import { history } from 'umi';
import RightContent from '@/components/RightContent';
import Footer from '@/components/Footer';
import { queryCurrent } from './services/user';
import { getMenuData } from '@/utils/swaggerUtils';
import defaultSettings from '../config/defaultSettings';
import { SettingDrawerProps } from '@ant-design/pro-layout';
import * as allIcons from '@ant-design/icons/es';
import { MessageDescriptor } from '@ant-design/pro-layout/lib/typings';
// import { FireOutlined } from '@ant-design/icons';

export async function getInitialState(): Promise<{
  currentUser?: API.CurrentUser;
  menuData?: any;
  settings?: LayoutSettings;
  settingDrawer?: SettingDrawerProps;
}> {
  const menuData = await getMenuData("");
  window['menuData'] = menuData;
  // 如果是登录页面，不执行
  if (history.location.pathname !== '/user/login') {
    try {
      const currentUser = await queryCurrent();
      return {
        currentUser,
        menuData: menuData,
        settings: defaultSettings,
        settingDrawer: {
          hideCopyButton: true,
          hideHintAlert: true,
          hideColors: false
        }
      };
    } catch (error) {
      // history.push('/user/login');
    }
  }
  return {
    menuData: menuData,
    settings: defaultSettings,
  };
}


export const layout = ({
  initialState,
}: {
  initialState: {
    settings?: LayoutSettings,
    menuData: any,
  };
}): BasicLayoutProps => {

  const formtterIcon = (item: any) => {
    if (item.icon) {
      const { icon } = item;
      if (icon.type != null) {
        return;
      }
      const v4IconName = toHump(icon.replace(icon[0], (icon[0] + "").toUpperCase()));
      const NewIcon = allIcons[icon] || allIcons[''.concat(v4IconName, 'Outlined')];
      if (NewIcon) {
        try {
          item.icon = React.createElement(NewIcon);
        } catch (error) {
          console.log(error);
        }
      }
    }
  }

  const toHump = (name: string) =>
    name.replace(/-(\w)/g, (all, letter) => (letter + "").toUpperCase());
  const formatter = (data: any) => {
    if (data.length > 0) {
      for (let index in data) {//需要递归啊...草，查找 routes
        formtterIcon(data[index]);
      }
    }
    return data;
  };

  const menuDataRender = () => {
    if (initialState != undefined) {
      var menuData = initialState.menuData;
      var newdx = formatter(menuData);
      return newdx;
    } else {
      return [];
    }
  }
  return {
    rightContentRender: () => <RightContent />,
    // logo:<FireOutlined style={{color:'red'}}/>,
    disableContentMargin: false,
    footerRender: () => <Footer />,
    menuDataRender: menuDataRender,
    formatMessage: (message: MessageDescriptor) => {
      // debugger
      if (message.defaultMessage != undefined) {
        return message.defaultMessage;
      }
      return "?";
    },
    menuHeaderRender: undefined,
    ...initialState?.settings,
  };
};
